﻿using System;
using System.Collections.Generic;   
using System.Web;
using System.Web.UI;
using Entities;
using Entities.Enumeraciones;
//using System.Web.UI.WebControls;
using DevExpress.Web.ASPxUploadControl;
using Helpers;
using System.Drawing;
using System.IO;
using System.Threading;

public partial class pNegocioCambiarImagen : System.Web.UI.Page
{
    const string UploadDirectory = "~/ImagenesSubidas/";
    const string ThumbnailFileName = "logoNegocio.jpg";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["Email"] != null)
                Master.UsuarioRegistrado(Session["Email"].ToString());
            lblError.Visible = false;
            if (Request["IDNegocio"] == null)
                Response.Redirect("pDefault.aspx");

            if (Request["Imagen"] == "0")
            {
                BuscarPlanSelecionado(long.Parse(Request["IDNegocio"]));
                rpImagen.Visible = false;
                rpPlan.Visible = true;
            }
            else
            {
                rpImagen.Visible = true;
                rpPlan.Visible = false;
            }
        }
        if (ASPxCallbackPanel1.IsCallback)
        {
            // Intentionally pauses server-side processing, 
            // to demonstrate the Loading Panel functionality.
            Thread.Sleep(500);
        }
        string selectedValue = string.Empty;
        //if (IsPostBack && listBoxPlanDeServicio.SelectedItem != null)
        //{
        //    BuscarPlanSelecionado(long.Parse(listBoxPlanDeServicio.SelectedItem.Value.ToString()));
            
        //}

    }

    private void BuscarPlanSelecionado(long IDPlan)
    {
        NegocioPlanDeServicio oNp = NegocioPlanDeServicio.ObtenerPlanServicioVigente(IDPlan, System.DateTime.Now);
        PlanServicio oPlan = oNp.PlanDeServicio;
        lblNombrePlan.Text = oPlan.NombrePlan;
        lblDescripcion.Text = oPlan.Descripcion;
        lblInicio.Text = oNp.FechaDesde.ToShortDateString();
        lblFin.Text = oNp.FechaHasta.ToShortDateString();
        lblPrecioFinal0.Text = oNp.Precio.ToString();
    }

  

    protected void uplImage_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
    {
        e.CallbackData = SavePostedFile(e.UploadedFile);
    }

    string SavePostedFile(UploadedFile uploadedFile)
    {
        if (!uploadedFile.IsValid)
            return string.Empty;
        string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
        Image original = Image.FromStream(uploadedFile.FileContent);
        Image thumbnail = PhotoUtils.Inscribe(original, 100);
        PhotoUtils.SaveToJpeg(thumbnail, fileName);
        //Guarda la Imagen en la base de datos
        byte[] bytesLogo = PhotoUtils.Image2Bytes(thumbnail);
        long IdNegocio = long.Parse(Request["IDNegocio"]);
        Negocio.ModificarLogo(IdNegocio, bytesLogo);
        return ThumbnailFileName;
    }




    protected void btnSiguiente_Click1(object sender, EventArgs e)
    {
        Response.Redirect("pDefaultNegocio.aspx?IDNegocio=" + Request["IDNegocio"]);
    }
    protected void btnSiguiente0_Click(object sender, EventArgs e)
    {
        Response.Redirect("pDefaultNegocio.aspx?IDNegocio=" + Request["IDNegocio"]);
    }
}
